home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / TextPanel.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  144 lines

  1. /*
  2.  * @(#)TextPanel.java    1.6 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.border.*;
  23. import com.sun.java.swing.text.*;
  24. import com.sun.java.accessibility.*;
  25.  
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.util.*;
  29. import java.io.*;
  30.  
  31.  
  32. /**
  33.  * Text!
  34.  *
  35.  * @version 1.6 02/02/98
  36.  * @author Jeff Dinkins
  37.  * @author Peter Korn (accessibility support)
  38.  */
  39. public class TextPanel extends JPanel 
  40. {
  41.     // The Frame
  42.     SwingSet swing;
  43.  
  44.     public TextPanel(SwingSet swing) {
  45.     super(true);
  46.     this.swing = swing;
  47.     setBorder(new CompoundBorder(swing.loweredBorder, swing.emptyBorder10));
  48.  
  49.     JPanel textFields = SwingSet.createVerticalPanel(false);
  50.  
  51.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  52.  
  53.     JTextField field1 = new FixedTextField("George Washington", 15);
  54.     field1.getAccessibleContext().setAccessibleName("First text field");
  55.  
  56.     JTextField field2 = new FixedTextField("Thomas Jefferson", 15);
  57.     field2.setForeground(Color.red);
  58.     field2.getAccessibleContext().setAccessibleName("Second text field");
  59.  
  60.     JTextField field3 = new FixedTextField("Benjamin Franklin", 15);
  61.     field3.setBackground(new Color(200, 200, 255)); // cornflower blue
  62.     field3.getAccessibleContext().setAccessibleName("Third text field");
  63.  
  64.     // JTextField field4 = new FixedTextField("Thomas Payne", 15); 
  65.         // Thanks to Chris Paine for pointing out that I misspelled "Paine"). (-:
  66.     JTextField field4 = new FixedTextField("Thomas Paine", 15); 
  67.     field4.setForeground(Color.yellow);
  68.     field4.setBackground(new Color(200, 140, 80)); // pumpkin
  69.     field4.getAccessibleContext().setAccessibleName("Fourth text field");
  70.  
  71.     JTextField field5 = new FixedTextField("Abraham Lincoln", 15);
  72.     field5.setForeground(Color.green.brighter());
  73.     field5.setBackground(Color.black);
  74.     field5.getAccessibleContext().setAccessibleName("Fifth text field");
  75.  
  76.     JLabel label = (JLabel) textFields.add(new JLabel("Text Fields:"));
  77.     label.setFont(swing.boldFont);
  78.     label.setLabelFor(field1);
  79.     textFields.add(Box.createRigidArea(swing.vpad10));
  80.     textFields.add(field1);
  81.     textFields.add(Box.createRigidArea(swing.vpad5));
  82.     textFields.add(field2);
  83.     textFields.add(Box.createRigidArea(swing.vpad5));
  84.     textFields.add(field3);
  85.     textFields.add(Box.createRigidArea(swing.vpad5));
  86.     textFields.add(field4);
  87.     textFields.add(Box.createRigidArea(swing.vpad5));
  88.     textFields.add(field5);
  89.     textFields.add(Box.createHorizontalStrut(5));
  90.  
  91.     String text = LoadFile("Constitution.txt");
  92.  
  93.     JPanel textAreaPanel = SwingSet.createVerticalPanel(false);
  94.     label = (JLabel) textAreaPanel.add(new JLabel("Text Area:"));
  95.     label.setFont(swing.boldFont);
  96.     textAreaPanel.add(Box.createRigidArea(swing.vpad10));
  97.  
  98.     JPanel textWrapper = new JPanel(new BorderLayout());
  99.     textWrapper.setAlignmentX(LEFT_ALIGNMENT);
  100.      textWrapper.setBorder(swing.loweredBorder);
  101.  
  102.     textAreaPanel.add(textWrapper);
  103.  
  104.     JTextArea textArea = new JTextArea(text);
  105.     JScrollPane scroller = new JScrollPane() {
  106.             public Dimension getPreferredSize() {
  107.         return new Dimension(300,100);
  108.         }
  109.         public float getAlignmentX() {
  110.         return LEFT_ALIGNMENT;
  111.         }
  112.     };
  113.     scroller.getViewport().add(textArea);
  114.     textArea.setFont(new Font("Dialog", Font.PLAIN, 12));
  115.     textArea.getAccessibleContext().setAccessibleName("Editable text area");
  116.     label.setLabelFor(textArea);
  117.     textWrapper.add(scroller, BorderLayout.CENTER);
  118.  
  119.     add(Box.createRigidArea(swing.hpad10));
  120.     add(textFields);
  121.     add(Box.createRigidArea(swing.hpad10));
  122.     add(textAreaPanel);
  123.     }
  124.  
  125.  
  126.     class FixedTextField extends JTextField {
  127.     public FixedTextField(String text, int columns) {
  128.         super(text, columns);
  129.     }
  130.     public Dimension getMaximumSize() {
  131.         return getPreferredSize();
  132.     }
  133.     public float getAlignmentX() {
  134.         return LEFT_ALIGNMENT;
  135.     }
  136.     }
  137.  
  138.     public String LoadFile(String filename) {
  139.       return SwingSet.contentsOfFile(filename);
  140.     }
  141.     
  142.     
  143. }
  144.